<template>
  <el-card class="box-card">
    <!-- 1.0 标题 -->
    <template #header>
      <h3>修改密码</h3>
    </template>
    <!-- 2.0 内容表单 -->
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      size="mini"
    >
      <el-form-item label="原密码" prop="oldpass">
        <el-input v-model="ruleForm.oldpass" @blur="oldpwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { validatePass, checkPass1, checkPass2 } from '@/utils/validate.js'
import { checkoldpwd, editpwd } from '../../api/users.js'
import local from '../../utils/locl.js'
export default {
  data() {
    return {
      ruleForm: {
        oldpass: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        oldpass: [{ validator: validatePass, trigger: 'blur' }],
        pass: [{ validator: checkPass1(this), trigger: 'blur' }],
        checkPass: [{ validator: checkPass2(this, 'pass'), trigger: 'blur' }]
      },
      flag: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          if (!this.flag) {
            this.$message.error('抱歉')
            return
          }
          const data = await editpwd({
            newPwd: this.ruleForm.pass
          })
          // console.log(data)
          if (data.code === 0) {
            this.$message({
              message: data.msg,
              type: 'success'
            })
            setTimeout(() => {
              local.del('token')
              location.reload()
            }, 1500)
          }
        } else {
          this.$message.error('请按规则填写')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 原密码
    async oldpwd(e) {
      // console.log(e.target.value)
      if (e.target.value.trim().length !== 0) {
        const data = await checkoldpwd({
          oldPwd: e.target.value
        })
        // console.log(data)
        if (data.code === '00') {
          this.$message({
            type: 'success',
            message: data.msg
          })
          this.flag = true
        } else {
          this.$message.error(data.msg)
          this.flag = false
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-input {
  width: 250px;
}
</style>
